/*
 * MarshallingLoadTest.java
 *
 * Created on 26 de Maio de 2008, 23:13
 */
package br.ufpe.cin.perfmiddle.LoadApp;

import br.ufpe.cin.if1118.exerc02bib.interop.PDU;
import com.csvreader.CsvWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author  fabio
 */
public class MarshallingLoadTest extends javax.swing.JInternalFrame {

    /** Creates new form MarshallingLoadTest */
    public MarshallingLoadTest() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jcbLongLoad = new javax.swing.JCheckBox();
        jcbIntLoad = new javax.swing.JCheckBox();
        jcbCharLoad = new javax.swing.JCheckBox();
        jcbBooleanLoad = new javax.swing.JCheckBox();
        jcbStringLoad = new javax.swing.JCheckBox();
        jcbShortLoad = new javax.swing.JCheckBox();
        jcbUnmarshallingOption = new javax.swing.JCheckBox();
        jButton1 = new javax.swing.JButton();
        jProgressBar1 = new javax.swing.JProgressBar();
        jlStatusLine = new javax.swing.JLabel();
        jtfStringSize = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jtfRepeats = new javax.swing.JTextField();
        jtfFileName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setTitle("Marshalling Load Test");

        jcbLongLoad.setText("Long");

        jcbIntLoad.setText("Int");

        jcbCharLoad.setText("Char");

        jcbBooleanLoad.setText("Boolean");

        jcbStringLoad.setText("String");

        jcbShortLoad.setText("Short");

        jcbUnmarshallingOption.setText("With Unmarshalling");

        jButton1.setText("Go!");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jProgressBar1.setStringPainted(true);

        jlStatusLine.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jlStatusLine.setText("Configure Load and Go!");
        jlStatusLine.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        jtfStringSize.setColumns(10);

        jLabel6.setText("Text:");

        jLabel1.setText("# of Repeats:");

        jtfRepeats.setColumns(10);

        jLabel2.setText("Salvar no arquivo:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jcbShortLoad)
                    .addComponent(jlStatusLine, javax.swing.GroupLayout.DEFAULT_SIZE, 443, Short.MAX_VALUE)
                    .addComponent(jProgressBar1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 443, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jcbIntLoad, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jcbCharLoad, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jcbBooleanLoad, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jcbStringLoad, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jcbLongLoad)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtfStringSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(51, 51, 51)
                                .addComponent(jcbUnmarshallingOption)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1)))
                        .addGap(28, 28, 28)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfRepeats, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(36, 36, 36))
                    .addComponent(jtfFileName, javax.swing.GroupLayout.DEFAULT_SIZE, 443, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(15, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfFileName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jcbLongLoad, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jcbIntLoad, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jcbCharLoad, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jcbBooleanLoad, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbStringLoad, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfStringSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jcbShortLoad, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbUnmarshallingOption)
                    .addComponent(jButton1)
                    .addComponent(jLabel1)
                    .addComponent(jtfRepeats, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jlStatusLine)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    /* Must select which type of load mix was configured and then call 
     * the marshalling/unmarshalling interfaces. Besides, need to collect 
     * execution time data and save it for latter analysis.
     * Should know the size of the test, to update the progress bar state
     */
    PDU pdu = new PDU();
    String name = jtfFileName.getText();
    ArrayList<String> record = new ArrayList<String>();
    CsvWriter writer = new CsvWriter(name);
    writer.setDelimiter(';');
    int repeats = Integer.parseInt(jtfRepeats.getText());
    jProgressBar1.setMaximum(repeats);
    
    for (int i = 0; i < repeats; i++) {
        if (!jcbUnmarshallingOption.isSelected()) {
            if (jcbBooleanLoad.isSelected()) {
                pdu.addBoolean(true);
                record.add(Long.toString(pdu.getDurationRun()));
            }
            if (jcbCharLoad.isSelected()) {
                pdu.addChar('e');
                record.add(Long.toString(pdu.getDurationRun()));
            }
            if (jcbShortLoad.isSelected()) {
                short value = (short) 21;
                pdu.addShort(value);
                record.add(Long.toString(pdu.getDurationRun()));
            }
            if (jcbIntLoad.isSelected()) {
                pdu.addInt(32);
                record.add(Long.toString(pdu.getDurationRun()));
            }
            if (jcbLongLoad.isSelected()) {
                long value = (long) 432;
                pdu.addLong(value);
                record.add(Long.toString(pdu.getDurationRun()));
            }
            if (jcbStringLoad.isSelected()) {
                int size = Integer.parseInt(jtfStringSize.getText());
                StringBuilder buffer = new StringBuilder();
                for (int j = 0; j < size; j++) {
                    buffer.append("a");
                }
                pdu.addString(buffer.toString());
                record.add(Long.toString(pdu.getDurationRun()));
            }

            try {
                writer.writeRecord(record.toArray(new String[record.size()]));
            } catch (IOException ex) {
                Logger.getLogger(MarshallingLoadTest.class.getName()).log(Level.SEVERE, null, ex);
            }

        } else {
            // Falta colocar os unmarshalling
            if (jcbUnmarshallingOption.isSelected()) {
                long firstTime = 0, secondTime = 0;
                if (jcbBooleanLoad.isSelected()) {
                    
                    pdu.addBoolean(true);
                    firstTime = pdu.getDurationRun();
                    pdu.getBoolean(i);
                    record.add(Long.toString(firstTime + secondTime));
                }
                if (jcbCharLoad.isSelected()) {
                    pdu.addChar('e');
                    firstTime = pdu.getDurationRun();
                    record.add(Long.toString(firstTime + secondTime));
                }
                if (jcbShortLoad.isSelected()) {
                    short value = (short) 21;
                    pdu.addShort(value);
                    firstTime = pdu.getDurationRun();
                    record.add(Long.toString(firstTime + secondTime));
                }
                if (jcbIntLoad.isSelected()) {
                    pdu.addInt(32);
                    firstTime = pdu.getDurationRun();
                    record.add(Long.toString(firstTime + secondTime));
                }
                if (jcbLongLoad.isSelected()) {
                    long value = (long) 432;
                    pdu.addLong(value);
                    firstTime = pdu.getDurationRun();
                    record.add(Long.toString(firstTime + secondTime));
                }
                if (jcbStringLoad.isSelected()) {
                    int size = Integer.parseInt(jtfStringSize.getText());
                    StringBuilder buffer = new StringBuilder();
                    for (int j = 0; j < size; j++) {
                        buffer.append("a");
                    }
                    pdu.addString(buffer.toString());
                    firstTime = pdu.getDurationRun();
                    record.add(Long.toString(firstTime + secondTime));
                }
                try {
                    writer.writeRecord(record.toArray(new String[record.size()]));
                } catch (IOException ex) {
                    Logger.getLogger(MarshallingLoadTest.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        record.clear();
        jProgressBar1.getModel().setValue(i);
        jProgressBar1.repaint();
    }
    writer.flush();
    writer.close();
    JOptionPane.showMessageDialog(this, "Test Finished", "Message", JOptionPane.INFORMATION_MESSAGE);
}//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JCheckBox jcbBooleanLoad;
    private javax.swing.JCheckBox jcbCharLoad;
    private javax.swing.JCheckBox jcbIntLoad;
    private javax.swing.JCheckBox jcbLongLoad;
    private javax.swing.JCheckBox jcbShortLoad;
    private javax.swing.JCheckBox jcbStringLoad;
    private javax.swing.JCheckBox jcbUnmarshallingOption;
    private javax.swing.JLabel jlStatusLine;
    private javax.swing.JTextField jtfFileName;
    private javax.swing.JTextField jtfRepeats;
    private javax.swing.JTextField jtfStringSize;
    // End of variables declaration//GEN-END:variables
}
